package com.flexcloudsdk.framework.core
{
	import flash.events.EventDispatcher;
	import mx.rpc.AbstractOperation;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	import mx.rpc.http.HTTPService;
	import mx.rpc.remoting.RemoteObject;

	public class CloudBase extends EventDispatcher
	{
		public var remoteObject:RemoteObject;
		public var httpService:HTTPService;
		public var lastResult:Object;
		public var lastError:Object;
		
		//-------------
		// properties
		//--------------

		//-------------
		// appName
		//--------------
		private var _appName:String = "";
		public function get appName():String
		{
			return _appName;
		}
		public function set appName(value:String):void
		{
			_appName = value;
		}
		
		//----------------
		// appUrl
		//----------------
		private var _appUrl:String = "";
		public function get appUrl():String
		{
			return _appUrl;
		}
		public function set appUrl(value:String):void
		{
			_appUrl = value;
		}

		//---------------
		// domain
		//---------------
		private var _domain:String="appspot.com";
		public function get domain():String
		{
			return _domain;			
		}
		public function set domain(value:String):void
		{
			_domain = value;
		}

		//---------------
		// localhost
		//---------------
		private var _localhost:Boolean = false;
		public function get localhost():Boolean
		{
			return _localhost;
		}
		public function set localhost(value:Boolean):void
		{
			if (_localhost == value)
				return;
			_localhost = value;
		}
		
	
		//--------------
		// serviceName
		//---------------
		private var _serviceName:String = "";
		public function get serviceName():String
		{
			return _serviceName;
		}
		public function set serviceName(value:String):void
		{
			if (_serviceName == value)
				return;
			_serviceName = value;
		}

		//--------------
		// port
		//--------------
		private var _port:int = -1;
		public function get port():int
		{
			return _port;
		}
		public function set port(value:int):void
		{
			if (_port == value)
				return;
			_port = value;
		}

		//--------------
		// objectId
		//--------------
		private var  _objectId:String = "";
		public function get objectId():String
		{
			return 	_objectId;
		}
		public function set objectId(value:String):void
		{
			if (value == _objectId)
				return;
			_objectId = value;	
		}

		//----------------
		// useRemoting
		//----------------
		private var _useRemoting:Boolean;
		public function get useRemoting():Boolean
		{
			return _useRemoting;			
		}
		public function set useRemoting(value:Boolean):void
		{
			if (_useRemoting == value)
				return;
			_useRemoting = value;		
			if (_useRemoting)
			{	
				useHttp = false;
				httpService = null;
				remoteObject = null;
				remoteObject = new RemoteObject();
				remoteObject.addEventListener(FaultEvent.FAULT,   remotingFaultHandler);
				remoteObject.addEventListener(ResultEvent.RESULT, remotingResultHandler);
				remoteObject.concurrency = "multiple";
				remoteObject.destination = objectId;
				remoteObject.makeObjectsBindable = true;
				remoteObject.showBusyCursor = true;
			}
			else
			{
				useHttp = true;
				remoteObject = null;
			}
		}

		//-----------
		// useHttp
		//-----------
		private var _useHttp:Boolean = true;
		public function get useHttp():Boolean
		{
			return _useHttp;	
		}
		public function set useHttp(value:Boolean):void
		{
			if (_useHttp == value)
				return;
			_useHttp = value;	
			if (_useHttp)
			{	
				useRemoting = false; 
				httpService = null;
				httpService = new HTTPService();
				if (localhost && port > 0)
					httpService.url = "http://localhost:" + port + "/" + serviceName;
				else if (localhost && port == -1)
					httpService.url = "http://localhost:8888" + "/" + serviceName;
				else if (www == true && localhost == false)
					httpService.url = "http://www." + appName + "." + domain + "/" + serviceName;
				else if (www == false && localhost == false)
					httpService.url = "http://" + appName + "." + domain + "/" + serviceName;
				httpService.useProxy = false;
				httpService.showBusyCursor = true;	
				httpService.resultFormat="e4x";
				httpService.method = "POST";
				httpService.contentType = "application/x-www-form-urlencoded";
				httpService.addEventListener(ResultEvent.RESULT, httpResultHandler);
				httpService.addEventListener(FaultEvent.FAULT, httpFaultHandler);
			}
		}
		//-------------
		// www
		//-------------
		private var _www:Boolean=false;
		public function get www():Boolean
		{
			return _www;	
		}
		public function set www(value:Boolean):void
		{
			if (_www == value)
				return;
			_www = value;	
		}
		
		//-------------
		// constructor
		//--------------
		public function CloudBase()
		{
		}


		//--------------------
		// event handlers 
		//--------------------
		protected function remotingResultHandler(event:ResultEvent):void
		{
			trace(event.result.toString());
			lastResult = event.result;
			dispatchEvent(event.clone());
		}
		
		protected function remotingFaultHandler(event:FaultEvent):void
		{
			trace(event.fault.toString());
			lastError = event.fault;
			dispatchEvent(event.clone());
		}
		
		protected function httpResultHandler(event:ResultEvent):void
		{
			trace(event.result.toString());
			lastResult = event.result;
			dispatchEvent(event.clone());
		}
		
		protected function httpFaultHandler(event:FaultEvent):void
		{
			trace(event.fault.toString());
			lastError = event.fault;
			dispatchEvent(event.clone());
		}
		
	}
}